<?php

/**
 * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
 * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
 * Copyright (C) 2008 Martijn van der Kleijn <martijn.niji@gmail.com>
 * Copyright (C) 2008 Maslakov Alexander <jmas.ukraine@gmail.com>
 *
 * This file is part of Frog CMS.
 *
 * Frog CMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Frog CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Frog CMS.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Frog CMS has made an exception to the GNU General Public License for plugins.
 * See exception.txt for details and the full text.
 */

/**
 * @package frog
 * @subpackage install
 *
 * @author Philippe Archambault <philippe.archambault@gmail.com>
 * @author Martijn van der Kleijn <martijn.niji@gmail.com>
 * @author Maslakov Alexander <jmas.ukraine@gmail.com>
 * @version 0.1
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * @copyright Philippe Archambault, 2008
 */
 

/*
	Definition
*/
define( 'DEFAULT_ADMIN_USER', 'admin' );
define( 'CORE_ROOT', dirname(__FILE__).'/../frog' );

$config_file = '../config.php';

include('Template.php');

if (file_exists($config_file))
  include($config_file);

$msg = '';
$PDO = false;



/*
	Lets install this nice little CMS
*/
if( !defined('DEBUG') && isset($_POST['commit']) && (file_exists($config_file) && is_writable($config_file)) )
{
	/*
		User settings
	*/
    define('INSTALL_SEQUENCE', true);
    $admin_name = DEFAULT_ADMIN_USER;
    $admin_passwd = '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8';
    $admin_passwd_precrypt = 'password';
	
    $_POST['config']['use_pdo'] = class_exists('PDO');
	
	
	
    /*
		check if the PDO driver we need is installed
	*/
    if( $_POST['config']['use_pdo'] and !in_array($_POST['config']['db_driver'], PDO::getAvailableDrivers()) )
        $_POST['config']['use_pdo'] = false;

    $config_tmpl = new Template('config.tmpl');
    
    $config_tmpl->assign($_POST['config']);
    $config_content = $config_tmpl->fetch();
    
    file_put_contents($config_file, $config_content);
    $msg .= '<p class="success">Config file successfully written.</p>';
    
    include($config_file);
	
	
	
    /*
		setup admin name (default to 'admin')
	*/
    if( isset($_POST['config']['admin_username']) )
	{
        $admin_name = $_POST['config']['admin_username'];
        $admin_name = trim($admin_name);
		
        $admin_passwd_precrypt = '12'.dechex(rand(100000000, 4294967295)).'K';
        $admin_passwd = sha1($admin_passwd_precrypt);
    }
    
	
	
	/*
		Write DB
	*/
    if( USE_PDO )
    {
        try
		{
            $PDO = new PDO(DB_DSN, DB_USER, DB_PASS);
        }
		catch (PDOException $e)
		{
            $msg = '
				<p class="error">Frog has not been installed properly.</p>
				<p class="error">The following error has occured: <strong>'. $e->getMessage() .'</strong></p>
			';
            file_put_contents($config_file, '');
        }
    }
    elseif( $_POST['config']['db_driver'] == 'mysql' )
    {
        require_once( CORE_ROOT . '/libraries/DoLite.php' );
        $PDO = new DoLite(DB_DSN, DB_USER, DB_PASS);
    }
    else
    {
        $msg = '
			<p class="error">Frog has not been installed properly.</p>
			<p class="error">You need PDO and SQLite 3 drive to use SQLite 3.</p>
		';
    }
    
    if( $PDO )
    {
        $msg .= '<p class="success">Database connection successfull.</p>';
        
        include('schema_'.$_POST['config']['db_driver'].'.php');
        include('sql_data.php');
        
        $msg .= '
			<p class="success">Tables loaded successfully</p>
			<h3>You can login with:</h3>
			<p><strong>login</strong>: '. $admin_name .'</p>
			<p><strong>password</strong>: '.$admin_passwd_precrypt.'</p>
			<p><strong>at</strong>: <a href="../admin/">login page</a></p>
			<p>Please be aware: the password is generated by Frog, please use it to login to Frog and <strong>change your password</strong>!</p>
		';
    }
    else
	{
		$error = 'Unable to connect to the database! Tables are not loaded!';
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<title>Frog CMS &ndash; Install</title>
		
		<link href="install.css" media="screen" rel="stylesheet" type="text/css" />
		
		<script src="../admin/javascripts/jquery/jquery.js" type="text/javascript"></script>
		<script src="install.js" type="text/javascript"></script>
	</head>
	<body id="installation">
	
		<div id="header">
			<div id="site-title">Frog CMS</div>
		</div>
	
		<div id="main">
			<div id="content-wrapper">
				<div id="content">
				<!-- content -->
					
					<h1>Frog Installation</h1>

					<?php if( !file_exists($config_file) ): ?>
						<p class="error"><strong>Error</strong>: <em>config.php</em> doesn't exist!</p>
					<?php elseif( !is_writable($config_file) ): ?>
						<p class="error"><strong>Error</strong>: <em>config.php<em> must be writable!</p>
					<?php endif; ?>
					<?php if ( ! is_writable('../public/')): ?>
						<p class="error"><strong>Error</strong>: <em>public/</em> folder must be writable!</p>
					<?php endif; ?>
					
				<?php if( !defined('DEBUG') ): ?>
					
					<form action="index.php" method="post" class="dform">
						<div class="dform-area">
							<table>
								<tr>
									<th colspan="3"><h3>Database information</h3></th>
								</tr>
								<tr>
									<td class="dform-lable"><label for="config_db_driver">Database driver</label></td>
									<td class="dform-field">
										<select id="config_db_driver" class="input-select" name="config[db_driver]" onchange="db_driver_change( this[this.selectedIndex].value );">
											<option value="mysql">MySQL</option>
											<option value="sqlite">SQLite 3</option>
										</select>
									</td>
									<td class="dform-help">Required. PDO support and the SQLite 3 plugin are required to use SQLite 3.</td>
								</tr>
								<tr id="row_db_host">
									<td class="dform-lable"><label for="config_db_host">Database server</label></td>
									<td class="dform-field"><input class="input-text" id="config_db_host" maxlength="100" name="config[db_host]" size="100" type="text" value="localhost" /></td>
									<td class="dform-help">Required.</td>
								</tr>
								<tr id="row_db_port">
									<td class="dform-lable"><label for="config_db_port">Port</label></td>
									<td class="dform-field"><input class="input-text" id="config_db_port" maxlength="10" name="config[db_port]" size="100" type="text" value="3306" /></td>
									<td class="dform-help">Optional. Default: 3306</td>
								</tr>
								<tr id="row_db_user">
									<td class="dform-lable"><label for="config_db_user">Database user</label></td>
									<td class="dform-field"><input class="input-text" id="config_db_user" maxlength="255" name="config[db_user]" size="255" type="text" value="root" /></td>
									<td class="dform-help">Required.</td>
								</tr>
								<tr id="row_db_pass">
									<td class="dform-lable"><label class="optional" for="config_db_pass">Database password</label></td>
									<td class="dform-field"><input class="input-text" id="config_db_pass" maxlength="40" name="config[db_pass]" size="40" type="password" value="" /></td>
									<td class="dform-help">Optional. If there is no database password, leave it blank.</td>
								</tr>
								<tr id="row-db-name">
									<td class="dform-lable"><label for="config_db_name">Database name</label></td>
									<td class="dform-field"><input class="input-text" id="config_db_name" maxlength="40" name="config[db_name]" size="40" type="text" value="frog" /></td>
									<td class="dform-help" id="help_db_name">Required. You have to create a database manually and enter its name here.</td>
								</tr>
								<tr id="row_table_prefix">
									<td class="dform-lable"><label class="optional" for="config_table_prefix">Table prefix</label></td>
									<td class="dform-field"><input class="input-text" id="config_table_prefix" maxlength="40" name="config[table_prefix]" size="40" type="text" value="" /></td>
									<td class="dform-help">Optional. Usefull to prevent conflicts if you have, or plan to have, multiple Frog installations with a single database.</td>
								</tr>
								
								<tr>
									<th colspan="3"><h3>Other information</h3></th>
								</tr>
								
								<tr>
									<td class="dform-lable"><label class="optional" for="config_admin_username">Administrator username</label></td>
									<td class="dform-field"><input class="input-text" id="config_admin_username" maxlength="40" name="config[admin_username]" size="40" type="text" value="<?php echo DEFAULT_ADMIN_USER; ?>" /></td>
									<td class="dform-help">Required. Allows you to specify a custom username for the administrator. Default: admin</td>
								</tr>
								<tr>
									<td class="dform-lable"><label class="optional" for="config_url_suffix">URL suffix</label></td>
									<td class="dform-field"><input class="input-text" id="config_url_suffix" maxlength="40" name="config[url_suffix]" size="40" type="text" value=".html" /></td>
									<td class="dform-help">Optional. Add a suffix to simulate static html files.</td>
								</tr>
							</table>
						</div>
						
						<p class="dform-buttons">
							<input class="input-button" type="submit" name="commit" value="Install now">
						</p>
					</form>

				<?php else: ?>

					<div id="process">
						<?php echo $msg; ?>

					<?php if( isset($error) ): ?>
							<p class="error"><?php echo $error; ?></p>
							<p><a href="index.php">Click here and try again</a></p>
				    <?php else: ?>
						<h3><strong>Frog CMS</strong> is installed, <b>you should now:</b></h3>
						<ol>
							<li>Delete the <code>install/</code> folder!</li>
							<li>Remove all write permissions from the <code>config.php</code> file!</li>
							<li>Remove <code>changelog.txt</code> and similar files to enhance security.</li>
						</ol>
				    <?php endif; ?>
					</div>

				<?php endif; ?>
				
				<!-- /content -->
				</div>
			</div>
		</div>

		<div id="footer">
			<p>This system was made with <a href="http://www.php.net" target="_blank">PHP</a> and is powered by <a href="http://www.madebyfrog.com/">Frog CMS</a></p>
		</div>
		
	</body>
</html>
